package com.car.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.car.mapper.ComClassifyMapper;
import com.car.model.common.dtos.PageResponseResult;
import com.car.model.common.dtos.ResponseResult;
import com.car.model.common.enums.AppHttpCodeEnum;
import com.car.model.component.dtos.ComClassifyDto;
import com.car.model.component.pojos.ComClassify;
import com.car.model.component.pojos.ComClassify2;
import com.car.service.ComClassify2Service;
import com.car.service.ComClassifyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ComClassifyServiceImpl extends ServiceImpl<ComClassifyMapper, ComClassify> implements ComClassifyService {


    @Override
    public ResponseResult findByPage(ComClassifyDto dto) {
        if (dto == null){
            dto = new ComClassifyDto();
        }
        dto.checkParam();

        Page page = new Page(dto.getPage(),dto.getSize());
        LambdaQueryWrapper<ComClassify> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(dto.getCode()!=null,ComClassify::getCode,dto.getCode()).eq(dto.getName()!=null,ComClassify::getName,dto.getName());
        IPage iPage = page(page, wrapper);
        PageResponseResult result = new PageResponseResult(dto.getPage(),dto.getSize(),(int)iPage.getTotal());
        result.setData(iPage.getRecords());
        return ResponseResult.okResult(result);
    }

    @Autowired
    ComClassify2Service classify2Service;

    @Override
    public ResponseResult delete(Integer id) {
        if (id == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        ComClassify classify = getById(id);
        if (classify == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        ResponseResult<ComClassify2> classify2 = classify2Service.find2ClassifyById(id);
        if (classify2.getData() != null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NO_OPERATOR_AUTH,"已包含其他分类,无法删除");
        }
        removeById(classify.getId());
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult updateClassify(ComClassify classify) {
        if (classify == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        updateById(classify);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult saveClassify(ComClassify classify) {
        if (classify == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        ComClassify one = getOne(Wrappers.<ComClassify>lambdaQuery().eq(ComClassify::getCode, classify.getCode()));
        if (one != null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"配件分类编码重复,请重新输入");
        }
        save(classify);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
